#ifndef _MECHMANIA_STATE_HEADER_
#define _MECHMANIA_STATE_HEADER_
#include "mm14/Types.h"

namespace mm14{

enum Weapon_Types {MINER = 0, QUAD_LASER = 1, ION_CANNON = 2, YAMATO_GUN = 3, DOCTOR_DEVICE = 4};

class State
{
	private:
		int _orderno;
		mm14::GameState _game_state;
		mm14::GameTurn &_game_turn;
	public:
		State(mm14::GameState game_state, mm14::GameTurn &game_turn) : _game_state(game_state), _game_turn(game_turn)
		{
			_orderno = 0;
		}

		const int AppraiseShip(const mm14::Ship &ship) const
		{
			return 0;
		}

		const mm14::Ship BuildShip(const mm14::Engine &engine, const mm14::Hull &hull, const mm14::PowerCore &power_core, const mm14::Sensor &sensor, const std::vector<mm14::Weapon> &weapons) const
		{
			mm14::Ship ship;
			ship.engine() = engine;
			ship.hull() = hull;
			ship.powerCore() = power_core;
			ship.sensor() = sensor;
			ship.weapons() = weapons;
			return ship;
		}

		void BuyShip(mm14::Ship ship)
		{
			ship.orderno() = _orderno++;
			_game_turn.builds().push_back(ship);
		}

		void MoveShip(const mm14::Ship &ship, const int &x_pos, const int &y_pos)
		{
			mm14::Move move;
			move.ship() = ship.id();
			move.locX() = x_pos;
			move.locY() = y_pos;

			move.orderno() = _orderno++;
			_game_turn.moves().push_back(move);
		}

		void GiveTribute(const mm14::Player &player, const int &money)
		{
			mm14::Tribute tribute;
			tribute.type() = goldTributeType;
			tribute.player() = player.id();
			tribute.value() = money;

			tribute.orderno() = _orderno++;
			_game_turn.tributes().push_back(tribute);
		}

		void GiveTribute(const mm14::Player &player, const mm14::Ship &ship)
		{
			mm14::Tribute tribute;
			tribute.type() = shipTributeType;
			tribute.player() = player.id();
			tribute.value() = ship.id();

			tribute.orderno() = _orderno++;
			_game_turn.tributes().push_back(tribute);
		}

		void MineAsteroid(const mm14::Ship &ship, const mm14::Asteroid &asteroid)
		{
			mm14::Mine mine;
			mine.asteroid() = asteroid.id();
			mine.miner() = ship.id();

			mine.orderno() = _orderno++;
			_game_turn.mines().push_back(mine);
		}

		void AttackShip(const mm14::Ship &ship, const mm14::Ship &enemy_ship, const mm14::Weapon &weapon)
		{
			mm14::Attack attack;
			attack.attacker() = ship.id();
			attack.attackee() = enemy_ship.id();
			attack.weapon() = weapon.type();

			attack.orderno() = _orderno++;
			_game_turn.attacks().push_back(attack);
		}

		void AuctionShip(const mm14::Ship &ship, const std::vector<mm14::Player> &blacklist, const int &starting_bid)
		{
			mm14::Auction auction;
			auction.ship() = ship.id();
			auction.blacklists() = blacklist;
			auction.startingBid() = starting_bid;

			auction.orderno() = _orderno++;
			_game_turn.auctions().push_back(auction);
		}

		void BidOnAuction(const mm14::Auction &auction, const int &amount)
		{
			mm14::Bid bid;
			bid.auction() = auction.id();
			bid.amount() = amount;

			bid.orderno() = _orderno++;
			_game_turn.bids().push_back(bid);
		}

		void BuyStock(const mm14::Player &player, const int &amount)
		{
			mm14::Stock stock;
			stock.company() = player.id();
			stock.percent() = amount;

			stock.orderno() = _orderno++;
			_game_turn.buyStocks().push_back(stock);
		}

		void SellStock(const mm14::Player &player, const int &amount)
		{
			mm14::Stock stock;
			stock.company() = player.id();
			stock.percent() = amount;

			stock.orderno() = _orderno++;
			_game_turn.sellStocks().push_back(stock);

		}

		const std::vector<mm14::Player> GetPlayers() const
		{
			return _game_state.players();
		}

		const std::vector<mm14::Ship> GetShips() const
		{
			return _game_state.ships();
		}

		const std::vector<mm14::Ship> GetMyShips() const
		{
			int my_id = GetMyId();
			std::vector<mm14::Ship> my_ships;
			std::vector<mm14::Ship> ships = _game_state.ships();
			for (std::vector<mm14::Ship>::const_iterator i = ships.begin(); i != ships.end(); i++)
			{
				if (i->owner() == my_id )
				{
					Ship s = *i;
					my_ships.push_back(s);
				}
			}

			return my_ships;
		}

		const std::vector<mm14::Asteroid> GetAsteroids() const
		{
			return _game_state.asteroids();
		}

		const std::vector<mm14::Auction> GetAuctions() const
		{
			return _game_state.auctions();
		}

		const std::vector<mm14::Stock> GetStocks() const
		{
			return _game_state.stocks();
		}

		const int GetMyId() const
		{
			int id;
			std::vector<mm14::Player> players = _game_state.players();
			for (std::vector<mm14::Player>::const_iterator i = players.begin(); i != players.end(); i++)
			{
				if (i->name() == _game_turn.username())
				{
					id = i->id();
				}
			}
			return id;
		}

		const mm14::Player GetMyself() const
		{
			mm14::Player player;
			for (std::vector<mm14::Player>::const_iterator i = _game_state.players().begin(); i != _game_state.players().end(); i++)
			{
				if (i->id() == GetMyId())
				{
					player = *i;
				}
			}
			return player;
		}

};

}

#endif

